package com.frxuexi.visit.view

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.VisitBeanItem
import com.frxuexi.lib_base.http.Const
import com.frxuexi.lib_base.http.RequestType
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.lib_base.utils.GlideUtils
import com.frxuexi.visit.R
import com.frxuexi.visit.VisitIntent
import com.frxuexi.visit.VisitViewModel
import com.frxuexi.visit.databinding.ActivityVisitAddBinding
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/visit/VisitAddActivity")
@AndroidEntryPoint
class VisitAddActivity : BaseActivity<ActivityVisitAddBinding,VisitIntent,VisitViewModel>() {
    override val binding: ActivityVisitAddBinding by lazy { ActivityVisitAddBinding.inflate(layoutInflater) }
    override val vm: VisitViewModel by viewModels<VisitViewModel>()
    val imgs= mutableListOf<String>()
    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                     is UiState.onSuccess<*> ->{
                         when(it.type){
                             RequestType.UPLOAD->{
                                 ToastUtils.showLong("上传成功")
                                 val img = it.data as String
                                 val fname = img.split(":")[1]
                                 imgs.add("http://10.161.9.80:2209/images/visit/${
                                     SPUtils.getInstance().getInt(
                                         Const.UID,-1)}/${fname}")
                             }
                             else->{
                                 ToastUtils.showLong("新增预约成功")
                                 finish()
                             }
                         }

                     }


                    else -> {}
                }
            }
        }
    }

    override fun initView() {

        binding.visitaddIvDate.setOnClickListener {
            showDateDialog {
                binding.visitaddDate.setText(it)
            }
        }

        binding.visitaddIvStart.setOnClickListener {
            showTimeDialog {
                binding.visitaddStart.setText(it)
            }
        }

        binding.visitaddIvEnd.setOnClickListener {
            showTimeDialog {
                binding.visitaddEnd.setText(it)
            }
        }

        binding.visitaddSwitch.setOnCheckedChangeListener { buttonView, isChecked ->
            binding.visitaddCar.isEnabled=isChecked
        }

        binding.visitaddSave.setOnClickListener {
            val name = binding.visitaddName.text.toString()
            val phone = binding.visitaddPhone.text.toString()
            val card = binding.visitaddCardId.text.toString()
            val date = binding.visitaddDate.text.toString()
            val start = binding.visitaddStart.text.toString()
            val purpose = binding.visitaddPurpose.text.toString()
            val car = binding.visitaddCar.text.toString()
            val people = binding.visitaddPeople.text.toString()
            val item = VisitBeanItem(
                "$date $start",
                false,
                imgs,
                people.toInt(),
                null,
                card,
                phone,
                car,
                purpose,
                binding.visitaddSwitch.isChecked,
                "待审核",
                name,0
            )
            vm.sendIntent(VisitIntent.addVisit(item))
        }

        binding.visitaddIv.setOnClickListener {
            PictureSelector.create(this,PictureSelector.SELECT_REQUEST_CODE).selectPicture()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==PictureSelector.SELECT_REQUEST_CODE && resultCode== RESULT_OK){
            val bean = data?.extras?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            GlideUtils.loadRoundImage(bean?.path!!,binding.visitaddIv)
            val split = bean.path.split("/")
            val name = split.get(split.lastIndex)
            vm.sendIntent(VisitIntent.upload(name,bean.path))
        }
    }

}